package cn.iocoder.yudao.module.alipay.service.surname;

import cn.iocoder.yudao.module.alipay.dal.dataobject.sensitivenems.SensitiveNemsDO;
import cn.iocoder.yudao.module.alipay.dal.mysql.sensitivenems.SensitiveNemsMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.admin.surname.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.surname.SurnameDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.surname.SurnameConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.surname.SurnameMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * 姓氏 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SurnameServiceImpl implements SurnameService {

    @Resource
    private SurnameMapper surnameMapper;

    @Resource
    private SensitiveNemsMapper sensitiveNemsMapper;

    @Override
    public Long createSurname(SurnameCreateReqVO createReqVO) {
        // 插入
        SurnameDO surname = SurnameConvert.INSTANCE.convert(createReqVO);
        surnameMapper.insert(surname);
        // 返回
        return surname.getId();
    }

    @Override
    public void updateSurname(SurnameUpdateReqVO updateReqVO) {
        // 校验存在
        validateSurnameExists(updateReqVO.getId());
        // 更新
        SurnameDO updateObj = SurnameConvert.INSTANCE.convert(updateReqVO);
        surnameMapper.updateById(updateObj);
    }

    @Override
    public void deleteSurname(Long id) {
        // 校验存在
        validateSurnameExists(id);
        // 删除
        surnameMapper.deleteById(id);
    }

    private void validateSurnameExists(Long id) {
        if (surnameMapper.selectById(id) == null) {
            throw exception(SURNAME_NOT_EXISTS);
        }
    }

    @Override
    public SurnameDO getSurname(Long id) {
        return surnameMapper.selectById(id);
    }

    @Override
    public List<SurnameDO> getSurnameList(Collection<Long> ids) {
        return surnameMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SurnameDO> getSurnamePage(SurnamePageReqVO pageReqVO) {
        return surnameMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SurnameDO> getSurnameList(SurnameExportReqVO exportReqVO) {
        return surnameMapper.selectList(exportReqVO);
    }

    @Override
    public SurnameDO getByName(String name) {
        return surnameMapper.getByName(name);
    }

    @Override
    public SensitiveNemsDO validCellValue(String sensitiveTxt) {
        return sensitiveNemsMapper.selectBySensitiveTxt(sensitiveTxt);
    }

}
